package com.kaf.springbootollama.controller;

import com.kaf.springbootollama.domain.ChatResult;
import com.kaf.springbootollama.service.Impl.OllamaServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/ollama")
public class OllamaController {

    private static final Logger log = LoggerFactory.getLogger(OllamaController.class);

    @Autowired
    private OllamaServiceImpl ollamaServiceImpl;

    @GetMapping("/chat/v3")
    public String ollamaChat(@RequestParam String msg){
        log.info("msg:{}",msg);
        return ollamaServiceImpl.chat(msg);
    }

    @PostMapping("/chat/v4")
    public ChatResult ollamaChatV4(@RequestParam String msg, @RequestParam String model){
        log.info("msg:{}",msg);
        log.info("model:{}",model);
        return ollamaServiceImpl.chat(msg,model);
    }

    @PostMapping("/chat/v5")
    public ChatResult ollamaChatV5(@RequestBody HashMap<String,Object> msgMap){
        if(msgMap == null || msgMap.isEmpty()){
            return new ChatResult("请输入chat内容");
        }
        String msg = (String) msgMap.get("msg");
        String model = (String) msgMap.get("model");
        log.info("msg:{}",msg);
        log.info("model:{}",model);
        return ollamaServiceImpl.chat(msg,model);
    }

    /*
    @Autowired
    private OllamaChatModel chatModel;


    @GetMapping("/chat/v1")
    public String ollamaChat(@RequestParam String msg){
        log.info("msg:{}",msg);
        if(StringUtils.isBlank(msg)){
            return "请输入chat内容";
        }
        String model = "qwen2.5:7b";
        Prompt prompt = new Prompt(
                msg,
                OllamaOptions.builder()
                        .withModel(model)
                        .withTemperature(0.4)
                        .build()
        );
        ChatResponse response = chatModel.call(prompt);
        log.info("response:{}",response.toString());
        return response.getResult().getOutput().getContent();
    }


    @PostMapping("/chat/v2")
    public Object ollamaChatV2(@RequestParam String msg,@RequestParam String model){
        log.info("msg:{}",msg);
        if(StringUtils.isBlank(msg)){
            return "请输入chat内容";
        }
        if(StringUtils.isBlank(model)){
            model = "qwen2.5:7b";
        }
        //String model = "qwen2.5:7b";
        Prompt prompt = new Prompt(
                msg,
                OllamaOptions.builder()
                        .withModel(model)
                        .withTemperature(0.4)
                        .build()
        );
        long startTime = System.nanoTime();// 记录开始时间
        ChatResponse response = chatModel.call(prompt);
        long endTime = System.nanoTime();// 记录结束时间

        // 计算并打印耗时
        double timeTaken = (endTime - startTime) / 1e9; // 将纳秒转换为秒
        log.info("耗时: " + timeTaken + " 秒");
        log.info("response:{}",response.toString());
        return response.getResult();
    }

     */
}
